using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using System.IO;
using System.Text.RegularExpressions;
using Jasongozy.Copor;
using System.Threading;
using System.Xml;
using Jasongozy.Alaton;
namespace Copor.Test.UnitTest {
    [TestFixture]
    public class OtherTest {
        [Test]
        public void DictionaryTest() {
            Dictionary<int, string> dic = new Dictionary<int, string>();
            for (int i = 0; i < 10; i++) {
                dic[i] = "dsa";
            }
            Assert.AreEqual(10, dic.Count);
        }

        [Test]
        public void RegexTest() {
            Regex r = new Regex(@"#{(\w+)\(([^#]*)\)}(?= *)(?!{)", RegexOptions.Compiled);
            string content = "#{GetFullColumnType(0)} {@MethodArgumentPrefix}{$Columns[0]}List,#{GetArgusForPK()}";
            Assert.AreEqual(true, r.IsMatch(content));
        }

        [Test]
        public void CheckDirTest() {
            string path = null;
            if (!Directory.Exists(path)) {
                Directory.CreateDirectory(path);
            }
        }

        [Test]
        public void TempParamRegexTest() {
            string input = "${name}";
            Match m = RegexMatch.FUNC_VAR.Match(input);

            string res = m.Groups[1].Value;
            Assert.AreEqual("name", res);
        }

        [Test]
        public void ListThreadSafe() {
            List<int> temp = new List<int>();
            Thread t1 = new Thread(delegate() {
                for (int i = 0; i < 100; i++) {
                    Thread.Sleep(50);
                    temp.Add(i);
                    Console.WriteLine(temp.Count);
                }
            });
            t1.Start();
            //Thread t2 = new Thread(delegate() {
            //    Thread.Sleep(100);
            //    while (temp.Count > 0) {
            //        Thread.Sleep(100);
            //        temp.RemoveAt(0);
            //    }
            //});
            //t2.Start();
            Console.WriteLine(1);
            //Thread t3 = new Thread(delegate() {
            //    while (temp.Count > 0) {
            //        Console.WriteLine(temp.Count);
            //    }
            //});
            //t3.Start();
        }

        [Test]
        public void XPathAposTest() {
            string content = "<Item><Value Name='22&apos;'/><Value Name='33&quot;' /><Value Name='&apos;44&quot;' /></Item>";
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(content);
            Console.WriteLine(doc.OuterXml);
            string xpath = "Item/Value[@Name='22'']";

            XmlNode n = doc.SelectSingleNode(xpath);
            Console.WriteLine(n.OuterXml);


            xpath = "Item/Value[@Name='33\"']";

             n = doc.SelectSingleNode(xpath);
            Console.WriteLine(n.OuterXml);
        }
    }
}
